package com.android.quiz;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Vector;
import com.google.gdata.client.spreadsheet.SpreadsheetService;
import com.google.gdata.data.spreadsheet.CellEntry;
import com.google.gdata.data.spreadsheet.CellFeed;
import com.google.gdata.data.spreadsheet.SpreadsheetEntry;
import com.google.gdata.data.spreadsheet.SpreadsheetFeed;
import com.google.gdata.data.spreadsheet.WorksheetEntry;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;
import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;

public class GoogleDatabase extends Activity {
	
	SQLiteDatabase database;
	List<WorksheetEntry> worksheets = null;
	URL cellFeedUrl = null;
	Helper question =  new Helper();
	Vector<Helper> DB = new Vector<Helper>();
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        new getdata().execute(database);
        finish();
	}
	
	public class getdata extends AsyncTask<SQLiteDatabase, Void,  Vector<Helper>>{

		@Override
		protected Vector<Helper> doInBackground(SQLiteDatabase... database) {
			
			try {
				SpreadsheetService myService = new SpreadsheetService("tweat");
				myService.setUserCredentials("jlgvasu@gmail.com", "quizmaster");
				URL metafeedUrl = new URL("https://spreadsheets.google.com/feeds/spreadsheets/private/full");
				SpreadsheetFeed feed = myService.getFeed(metafeedUrl, SpreadsheetFeed.class);
				List<SpreadsheetEntry> spreadsheets = feed.getEntries();
				
				for (int i = 0; i < spreadsheets.size(); i++) {
					  SpreadsheetEntry entry = spreadsheets.get(i);
					  if(entry.getTitle().getPlainText().equals("QuizMaster"))  //System.out.println("\t" + entry.getTitle().getPlainText());
			    			worksheets = entry.getWorksheets();
					}
				
				for (int i = 0; i < worksheets.size(); i++) {
		    		  WorksheetEntry worksheet = worksheets.get(i);
		    		  String title = worksheet.getTitle().getPlainText();
		    		  if(title.equals("Database")){
		    			  cellFeedUrl = worksheet.getCellFeedUrl();
		    			  System.out.println("\t" + title);
		    		  }
		    		  
		    		  CellFeed feed1 = myService.getFeed(cellFeedUrl, CellFeed.class);
		    		  int count =0;
		    		  for (CellEntry cell : feed1.getEntries()){
		    			  
		    			  if(count%6 == 0) question.setQuestion(cell.getCell().getValue());
		    			  
		    			  else if(count%6 == 1) question.setOption1(cell.getCell().getValue());
		    			  else if(count%6 == 2) question.setOption2(cell.getCell().getValue());
		    			  else if(count%6 == 3) question.setOption3(cell.getCell().getValue());
		    			  else if(count%6 == 4) question.setOption4(cell.getCell().getValue());
		    			  
		    			  else if(count%6 == 5){
		    				  question.setAnswer(cell.getCell().getValue());
		    				  DB.add(question);
		    				  Log.d("Obtained Data", question.getQuestion() + "--:-- " + question.getOption1() + "--:-- " + question.getOption2() +
				    				  "--:-- " + question.getOption3() + "--:-- " + question.getOption4() + "--:-- " + question.getAnswer());
		    				  question =  new Helper();
		    			  }
			    		  count++;
		    		  }
		    		  }
				
			}
			catch (AuthenticationException e) {  e.printStackTrace();  } 
			catch (MalformedURLException e) {  e.printStackTrace();  }
			catch (IOException e) {  e.printStackTrace();  }
			catch (ServiceException e) {  e.printStackTrace();  }
			
			return DB;
		}
		
		@Override
		protected void onPostExecute(Vector<Helper> DB){
			database = openOrCreateDatabase("QuizMaster", MODE_PRIVATE, null);
			database.execSQL("create table if not exists Questions(_id integer primary key Autoincrement, Question text not null, Opt1 Text not null,  Opt2 Text not null, Opt3 Text not null, Opt4 Text not null, Answer text not null);");
	        
			Log.d("Vector Size is :", DB.size()+"");
			for(int i =0; i< DB.size(); i++){
				Helper ques = DB.get(i);
				
				Log.d("Data in vector is : ", ques.getQuestion() + "--:-- " + ques.getOption1() + "--:-- " + ques.getOption2() +
	    				  "--:-- " + ques.getOption3() + "--:-- " + ques.getOption4() + "--:-- " + ques.getAnswer());
				
				database.execSQL("insert into Questions(Question, Opt1, Opt2, Opt3, Opt4, Answer) " +
						"values('" + ques.getQuestion() + "','" + ques.getOption1() + "','" + ques.getOption2() + "', '" +
						ques.getOption3() + "', '" + ques.getOption4() + "','" + ques.getAnswer() + "');");
			}
			database.close();
	        Log.d("Database", "Data has been inserted");
	        
		}
		
	}

}
